package com.iflytek.autoCompareTool.utils;

import org.csource.common.MyException;
import org.csource.fastdfs.StorageClient1;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author llchen12
 * @date 2018/5/30
 */
public class FastdfsUtil {

    private static FastdfsPool fastdfsPool;

    private static Logger logger = LoggerFactory.getLogger(FastdfsUtil.class);

    private static final byte[] EMPTY_BYTES = new byte[0];

    static {
        fastdfsPool = new FastdfsPool("fastdfs-client.properties");
    }


    public static String upload(byte[] bytes, String extName) {

        StorageClient1 storageClient1 = null;
        String result = null;
        try {
            storageClient1 = fastdfsPool.getStorageClient1();
            result = storageClient1.upload_file1(bytes, extName, null);
        } catch (IOException | MyException e) {
            logger.error("upload file error ：", e);
        } finally {
            fastdfsPool.returnStorageClient1(storageClient1);
        }
        return result;
    }


    public static String upload(String filePath) {

        StorageClient1 storageClient1 = null;
        String result = null;
        try {
            storageClient1 = fastdfsPool.getStorageClient1();
            result = storageClient1.upload_file1(filePath, getFileExtName(filePath), null);
        } catch (IOException | MyException e) {
            logger.error("upload file error ：", e);
        } finally {
            fastdfsPool.returnStorageClient1(storageClient1);
        }
        return result;
    }



    public static byte[] download(String group, String remoteFile) {
        byte[] result = EMPTY_BYTES;
        StorageClient1 storageClient1 = null;
        try {
            storageClient1 = fastdfsPool.getStorageClient1();
            result = storageClient1.download_file(group, remoteFile);
        } catch (Exception e) {
            logger.error("download file->{} error ", remoteFile, e);
        } finally {
            fastdfsPool.returnStorageClient1(storageClient1);
        }
        return result;
    }

    public static byte[] download(String fileId) {
        int i = fileId.indexOf("/");
        String group = fileId.substring(0, i);
        String remoteFile = fileId.substring(i + 1);
        return download(group, remoteFile);
    }

    public static InputStream downloadToStream(String fileId) {
        return new ByteArrayInputStream(download(fileId));
    }

    public static void downloadToFile(String fileId, String target)  {
        try (FileOutputStream fos = new FileOutputStream(target)) {
            fos.write(download(fileId));
            fos.flush();
        }catch (IOException e){
            logger.error("download to {} error",target,e);
        }
    }

    public static int delete(String fileId) {
        int i = fileId.indexOf("/");
        String group = fileId.substring(0, i);
        String remoteFile = fileId.substring(i + 1);
        return delete(group, remoteFile);
    }

    public static int delete(String group, String remoteFile) {
        int result = 0;
        StorageClient1 storageClient1 = null;
        try {
            storageClient1 = fastdfsPool.getStorageClient1();
            int i = storageClient1.delete_file(group, remoteFile);
            result = i == 0 ? 1 : 0;
        } catch (Exception e) {
            logger.error("delete file->{} error", remoteFile, e);
        } finally {
            fastdfsPool.returnStorageClient1(storageClient1);
        }
        return result;
    }


    public static String getFileExtName(String file) {
        return file.substring(file.lastIndexOf(".") + 1);
    }

    public static void main(String[] args) {
        //System.out.println(getFileExtName("/dcugbybd/cc/.jdd/s.jpg"));

        String fileId = upload("C:/Users/DELL-5490/Pictures/qxb/锁屏.jpg");
        fileId="group1/M00/03/5F/rBCaalsYll2AcRphAABiq0yfXnk06.xlsx";
        System.out.println(fileId);
        downloadToFile(fileId,"C:/Users/DELL-5490/Pictures/qxb/222.xlsx");
        //System.out.println(delete(fileId));

    }


}
